
/*
'===============================================================================
'  MP.LayerHelper.MP_OBS.BL.MbModulesManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.FactoryClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MP.Core.Library;

namespace MP.LayerHelper.MP_OBS.BLL
{
    public class MbModulesManager : MbModulesManagerBase
    {
        /// <summary>
        /// Purpose: Class constructor.
        /// </summary>
        public MbModulesManager()
        {
            // Nothing for now.
        }

        /// <summary>
        /// Method to create an instance of MbModulesManager
        /// </summary>
        /// <returns>An instant of MbModulesManager class</returns>
        public static MbModulesManager CreateInstant()
        {
            return new MbModulesManager();
        }

        public DataSet GetModuleDepart(Guid departId)
        {
            string strSQL = "PRC_GetModuleDepart";
            SqlParameter param = new SqlParameter("@DepartId", departId);
            return SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringOBS, CommandType.StoredProcedure, strSQL, param);
        }

        public void UpdateDepart(string moduleId, Guid departId, bool isEnable)
        {
            string strSQL = "Update mbModules " +
                            "Set DepartId=@DepartId, IsEnable=@IsEnable " +
                            "Where ModCode=@ModCode ";
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@ModCode", moduleId);
            param[1] = new SqlParameter("@DepartId", departId);
            param[2] = new SqlParameter("@IsEnable", isEnable);
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL, param);
        }

        public Guid GetDepartId(string moduleId)
        {
            string strSQL = "Select * " +
                            "From mbModules " +
                            "Where ModCode=@ModCode";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@ModCode", moduleId);
            DataTable tbl = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL, param);
            if (tbl != null && tbl.Rows.Count > 0)
            {
                if (!Convert.IsDBNull(tbl.Rows[0]["DepartId"]))
                {
                    return new Guid(tbl.Rows[0]["DepartId"].ToString());
                }
            }
            return Guid.Empty;
        }

        public DataTable GetByParent(string pCode, bool iPar)
        {
            string sql = " SELECT * " +
                         " FROM mbModules ";
            if (!string.IsNullOrEmpty(pCode))
            {
                sql += " WHERE ParentCode = @pCode ";
            }
            else
            {
                if (iPar)
                {
                    sql += " WHERE ParentCode IS NULL OR ParentCode = '' ";
                }
                else
                {
                    sql += " WHERE ParentCode IS NOT NULL OR ParentCode <> '' ";
                }
            }
            sql += " ORDER BY ParentCode ASC, ModName ASC ";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@pCode", pCode);
            return SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringOBS, CommandType.Text, sql, param);
        }
    }
}
